import json
import time

from selenium import webdriver
from appium import webdriver as appdriver
from selenium.webdriver.common.by import By


class UtilsDriver:
    mp_driver = None
    mis_driver = None
    app_driver = None
    is_mis_driver = True

    @classmethod
    def get_mp_driver(cls):
        if cls.mp_driver is None:
            cls.mp_driver = webdriver.Chrome()
            cls.mp_driver.maximize_window()
            cls.mp_driver.get("http://ttmp.research.itcast.cn/#/index")
        return cls.mp_driver

    @classmethod
    def quit_mp_driver(cls):
        if cls.mp_driver is not None:
            cls.get_mp_driver().quit()
            cls.mp_driver = None

    @classmethod
    def get_mis_driver(cls):
        if cls.mis_driver is None:
            cls.mis_driver = webdriver.Chrome()
            cls.mis_driver.maximize_window()
            cls.mis_driver.get("http://ttmis.research.itcast.cn/#/home")
        return cls.mis_driver

    @classmethod
    def quit_mis_driver(cls):
        if cls.mis_driver is not None and cls.is_mis_driver:
            cls.get_mis_driver().quit()
            cls.mis_driver = None

    @classmethod
    def get_app_driver(cls):
        # adb shell dumpsys window windows|findstr "usedApp"
        # aapt dumpsys badging 完整路径
        if cls.app_driver is None:
            devices_dict = {
                'platformName': 'Android',
                'platformVersion': '5.1',
                'deviceName': 'emulator-5554',
                'appPackage': 'com.itcast.toutiaoApp',
                'appActivity': '.MainActivity',
                'unicodeKeyboard': True,
                'resetKeyboard': True,
                'noReset': True
            }
            cls.app_driver = appdriver.Remote("http://localhost:4723/wd/hub", devices_dict)
        return cls.app_driver

    @classmethod
    def quit_app_driver(cls):
        if cls.app_driver is not None:
            cls.get_app_driver().quit()
            cls.app_driver = None


def select_box(driver, element, content_value):
    """
    封装下拉框
    :param driver:
    :param element:
    :param content_value:
    :return:
    """
    content = By.XPATH, "//*[text()='{}']".format(content_value)
    element.click()
    time.sleep(1)
    driver.find_element(*content).click()


def is_exist_element(driver, location):
    """
    判断页面元素是否存在
    :param driver:
    :param location:
    :return:
    """
    try:
        driver.find_element(*location)
        return True
    except Exception as e:
        return False


def get_json_data(filename):
    list_data = []
    with open(filename, encoding='utf8') as f:
        dict_data = json.load(f)
    for i in dict_data:
        for j in i.values():
            list_data.append(tuple(j.values()))
    return list_data


if __name__ == '__main__':
    # UtilsDriver.get_mp_driver()
    # UtilsDriver.quit_mp_driver()
    # UtilsDriver.get_mis_driver()
    # UtilsDriver.quit_mis_driver()
    # UtilsDriver.get_app_driver()
    # UtilsDriver.quit_app_driver()
    print(get_json_data('./data/json_data.json'))
